/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { sm2, sm3, sm4 } from './sm-crypto'
const message = "Firstly change challenges our adaptability. It tests our ability to navigate new situations and learn from them!";
let count = 1
function sm2_generateKeyPairHex_test() {
    let keypair;
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        keypair = sm2.generateKeyPairHex();
    }
    let end = Date.now();
    print(`sm-crypto-sm2_generateKeyPairHex: ${end - start} ms`)
    if(keypair.constructor!==Object){
        throw new Error('sm-crypto-sm2_generateKeyPairHex err')
    }
}
function sm2_doEncrypt_test() {
    let keypair = sm2.generateKeyPairHex();
    const publicKey = keypair.publicKey;
    const privateKey = keypair.privateKey;
    let encryptData;
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        encryptData = sm2.doEncrypt(message, publicKey, 1); // encrypt result
    }
    let end = Date.now();
    const decryptData = sm2.doDecrypt(encryptData, privateKey, 1); // decrypt result

    print(`sm-crypto-sm2_doEncrypt: ${end - start} ms`)
    if (decryptData !== 'Firstly change challenges our adaptability. It tests our ability to navigate new situations and learn from them!') {
        throw new Error('sm-crypto-sm2_doEncrypt err')
    }
}
function sm2_doDecrypt_test() {
    let keypair = sm2.generateKeyPairHex();
    const publicKey = keypair.publicKey;
    const privateKey = keypair.privateKey;
    let encryptData = sm2.doEncrypt(message, publicKey, 1); // encrypt result
    let decryptData;
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        decryptData = sm2.doDecrypt(encryptData, privateKey, 1); // decrypt result  
    }
    let end = Date.now();
    print(`sm-crypto-sm2_doDecrypt(encryptData, privateKey, cipherMode): ${end - start} ms`)
    if (decryptData !== 'Firstly change challenges our adaptability. It tests our ability to navigate new situations and learn from them!') {
        throw new Error('sm-crypto-sm2_doDecrypt err')
    }
}
function sm2_doDecrypt_test_01() {
    let keypair = sm2.generateKeyPairHex();
    const publicKey = keypair.publicKey;
    const privateKey = keypair.privateKey;
    let encryptData = sm2.doEncrypt(message, publicKey, 1); // encrypt result
    let decryptData;
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        decryptData = sm2.doDecrypt(encryptData, privateKey); // decrypt result  
    }
    let end = Date.now();
    print(`sm-crypto-sm2_doDecrypt(encryptData, privateKey): ${end - start} ms`)
    if (decryptData !== 'Firstly change challenges our adaptability. It tests our ability to navigate new situations and learn from them!') {
        throw new Error('sm-crypto-sm2_doDecrypt err')
    }
}
function sm2_doSignature_test() {
    let keypair = sm2.generateKeyPairHex();
    const publicKey = keypair.publicKey;
    const privateKey = keypair.privateKey;
    let sigValueHex6;
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        sigValueHex6 = sm2.doSignature(message, privateKey, {
            hash: true,
            publicKey,
            userId: "1234567812345678",
        });
    }
    let end = Date.now();
    print(`sm-crypto-sm2_doSignature: ${end - start} ms`)
    const verifyResult6 = sm2.doVerifySignature(message, sigValueHex6, publicKey, {
        hash: true,
        userId: "1234567812345678",
    });
    if (verifyResult6 !== true) {
        throw new Error('sm-crypto-sm2_doSignature err')
    }
}
function sm2_doVerifySignature_test() {
    let keypair = sm2.generateKeyPairHex();
    const publicKey = keypair.publicKey;
    const privateKey = keypair.privateKey;
    let sigValueHex6 = sm2.doSignature(message, privateKey, {
        hash: true,
        publicKey,
        userId: "1234567812345678",
    });
    let verifyResult6
    let start = Date.now();
    for (let i = 0; i < count; i++) {
        verifyResult6 = sm2.doVerifySignature(message, sigValueHex6, publicKey, {
            hash: true,
            userId: "1234567812345678",
        });
    }
    let end = Date.now();
    print(`sm-crypto-sm2_doVerifySignature: ${end - start} ms`)

    if (verifyResult6 !== true) {
        throw new Error('sm-crypto-sm2_doVerifySignature err')
    }
}
function sm3_test() {
    let str='Firstly, change challenges our adaptability. It tests our ability to navigate new situations and learn from them'
    let hmacData ;
    let start = Date.now();
    for (let i = 0; i < 1000; i++) {
        hmacData = sm3(str, {
            key: "daac25c1512fe50f79b0e4526b93f5c0e1460cef40b6dd44af13caec62e8c60e0d885f3c6d6fb51e530889e6fd4ac743a6d332e68a0f2a3923f42585dceb93e9",
        });
    }
    let end = Date.now();
    print(`sm-crypto_sm3: ${end - start} ms`)
    if(hmacData!='cf68e70ffa81ba2afeda852ddb1aa0c64e85f61554403c44b6bbd47ca7656a62'){
        throw new Error('sm-crypto-sm3 err')
    }
}

function sm4_encrypt_test() {
    const key = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10];
    let sm4EncryptData;
    let start = Date.now();
    for (let i = 0; i < 1000; i++) {
        sm4EncryptData = sm4.encrypt([
            0x01,
            0x23,
            0x45,
            0x67,
            0x89,
            0xab,
            0xcd,
            0xef,
            0xfe,
            0xdc,
            0xba,
            0x98,
            0x76,
            0x54,
            0x32,
            0x10,
        ], key);
    }
    let end = Date.now();
    print(`sm-crypto-sm4_encrypt: ${end - start} ms`)
    if (sm4EncryptData != '681edf34d206965e86b3e94f536e4246002a8a4efa863ccad024ac0300bb40d2') {
        throw new Error('sm-crypto-sm4_encrypt err')
    }
}

function sm4_decrypt_test() {
    const encryptData =
        "0e395deb10f6e8a17e17823e1fd9bd98a1bff1df508b5b8a1efb79ec633d1bb129432ac1b74972dbe97bab04f024e89c";
    const key1 = "0123456789abcdeffedcba9876543210";
    let out1;
    let start = Date.now();
    for (let i = 0; i < 1000; i++) {
        out1 = sm4.decrypt(encryptData, key1);
    }
    let end = Date.now();
    print(`sm-crypto-sm4_decrypt(inArray, key): ${end - start} ms`)
    if (out1 != 'hello world! 我是 juneandgreen.') {
        throw new Error('sm-crypto-sm4_decrypt err')
    }
}
function sm4_decrypt_test_01() {
    const encryptData =
        "0e395deb10f6e8a17e17823e1fd9bd98a1bff1df508b5b8a1efb79ec633d1bb129432ac1b74972dbe97bab04f024e89c";
    const key1 = "0123456789abcdeffedcba9876543210";
    let out1;
    let start = Date.now();
    for (let i = 0; i < 1000; i++) {
        out1 = sm4.decrypt(encryptData, key1, { padding: "none", output: "array" });
    }
    let end = Date.now();
    print(`sm-crypto-sm4_decrypt(inArray, key, options:{padding:"none",output:"array"}): ${end - start} ms`)
    if (JSON.stringify(out1) != '[104,101,108,108,111,32,119,111,114,108,100,33,32,230,136,145,230,152,175,32,106,117,110,101,97,110,100,103,114,101,101,110,46,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15]') {
        throw new Error('sm-crypto-sm4_decrypt err')
    }
}
function sm4_decrypt_test_02() {
    const encryptData =
        "0e395deb10f6e8a17e17823e1fd9bd98a1bff1df508b5b8a1efb79ec633d1bb129432ac1b74972dbe97bab04f024e89c";
    const key1 = "0123456789abcdeffedcba9876543210";
    let out1;
    let start = Date.now();
    for (let i = 0; i < 1000; i++) {
        out1 =  sm4.decrypt(encryptData, key1, { padding: "pkcs#5", output: "array" });
    }
    let end = Date.now();
    print(`sm-crypto-sm4_decrypt(inArray, key, options:{padding:"pkcs#5",output:"array"}): ${end - start} ms`)
    if (JSON.stringify(out1) != '[104,101,108,108,111,32,119,111,114,108,100,33,32,230,136,145,230,152,175,32,106,117,110,101,97,110,100,103,114,101,101,110,46]') {
        throw new Error('sm-crypto-sm4_decrypt err')
    }
}


sm2_generateKeyPairHex_test();
sm2_doEncrypt_test();
sm2_doDecrypt_test();
sm2_doDecrypt_test_01()
sm2_doSignature_test();
sm2_doVerifySignature_test();
sm3_test();
sm4_encrypt_test();
sm4_decrypt_test();
sm4_decrypt_test_01();
sm4_decrypt_test_02()
